package com.gimi.cloud.jimi.client.service.userInfo.impl;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.jimi.client.service.userInfo.UserExpressService;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.dao.dao.User.UserExpressDao;
import com.gimi.cloud.jimi.dto.userInfo.RdExpressInfoDTO;
import com.gimi.cloud.jimi.entity.userInfo.RdExpressInfoEntity;

@Service
@Transactional
public class UserExpressServiceImpl implements UserExpressService {

    @Autowired
    private UserExpressDao userExpressDao;
    private static final Logger logger = LoggerFactory.getLogger(UserExpressServiceImpl.class);

    @Override
    public void userExpressAddOrUpdate(RdExpressInfoDTO rdExpressInfoDTO) {
        Integer userId = rdExpressInfoDTO.getUserId();
        RdExpressInfoEntity record = new RdExpressInfoEntity();
        BeanUtils.copyProperties(rdExpressInfoDTO, record);
        int i = 0;
        //判断是新增还是编辑
        RdExpressInfoDTO expressInfoDTO = userExpressDao.getByUserId(userId);
        if (expressInfoDTO == null) {
            logger.info(String.format("新增用户%s收货地址为: %s %s", userId, record.getAddress(), record.getDetailAddress()));
            record.setIsDelete(0);
            record.setAddTime(new Date());
            i = userExpressDao.insertSelective(record);
        } else {
            logger.info(String.format("编辑用户%s收货地址为: %s %s", userId, record.getAddress(), record.getDetailAddress()));
            record.setUpdateTime(new Date());
            record.seteId(expressInfoDTO.geteId());
            i = userExpressDao.updateByPrimaryKeySelective(record);
        }
        if (i <= 0) {
            throw new MsgException(ResponseCodeEnum.INVALID, "操作失败");
        }
    }

    @Override
    public RdExpressInfoDTO getExpressInfoSingle(RdExpressInfoDTO rdExpressInfoDTO) {
        if (rdExpressInfoDTO.getUserId() == null || rdExpressInfoDTO.getUserId().equals(0)) {
            throw new MsgException(ResponseCodeEnum.INVALID, "userId不能为空");
        }
        return userExpressDao.getByUserId(rdExpressInfoDTO.getUserId());
    }

	/*@Override
    public void updateExpressInfo(RdExpressInfoDTO rdExpressInfoDTO) {
		RdExpressInfoEntity record = new RdExpressInfoEntity();
		BeanUtils.copyProperties(rdExpressInfoDTO, record);
		int i = userExpressDao.updateByPrimaryKeySelective(record);
		if (i <= 0) {
			throw new MsgException(ResponseCodeEnum.INVALID);
		}
	}
	//根据user_id查询配送信息列表(分页)
	@Override
	public PageInfo<RdExpressInfoEntity> ExpressPageListByUser(RdExpressInfoDTO rdExpressInfoDTO) {
		if (StringUtil.isBlank(rdExpressInfoDTO.getUserId()) || "".equals(rdExpressInfoDTO.getUserId())) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		PageHelper.startPage(rdExpressInfoDTO.getCurrentPage(), rdExpressInfoDTO.getPageSize());
	    List<RdExpressInfoEntity> list = userExpressDao.expressListByUser(rdExpressInfoDTO.getUserId()+"");
	    PageInfo<RdExpressInfoEntity> page = new PageInfo<>(list);
		return page;
	}*/

}
